/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mytdev.swing.actions;

import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A Swing abstract action based on resource bundle.
 * @author Yann D'Isanto
 */
abstract class ResourceAbstractAction extends AbstractAction {

    private static final Logger LOG = LoggerFactory.getLogger(ResourceAbstractAction.class);
    private String propertyPrefix;
    private ResourceBundle resources;

    /**
     * Instanciates a ResourceAbstractAction using the specified resource bundle.
     * @param resources the resource bundle.
     */
    ResourceAbstractAction(ResourceBundle resources) {
        this(resources, null);
    }

    /**
     * Instanciates a ResourceAbstractAction using the specified resource bundle 
     * and the specified property prefix.
     * @param resources the resource bundle.
     * @param propertyPrefix the property prefix.
     */
    ResourceAbstractAction(ResourceBundle resources, String propertyPrefix) {
        this.resources = resources;
        this.propertyPrefix = propertyPrefix != null ? propertyPrefix : getClass().getSimpleName();
        String value = getPropertyResourceValue(ActionProperties.NAME);
        if (value != null) {
            putValue(javax.swing.Action.NAME, value);
        }
        value = getPropertyResourceValue(ActionProperties.DESCRIPTION);
        if (value != null) {
            putValue(javax.swing.Action.SHORT_DESCRIPTION, value);
            putValue(javax.swing.Action.LONG_DESCRIPTION, value);
        }
        value = getPropertyResourceValue(ActionProperties.SHORT_DESCRIPTION);
        if (value != null) {
            putValue(javax.swing.Action.SHORT_DESCRIPTION, value);
        }
        value = getPropertyResourceValue(ActionProperties.LONG_DESCRIPTION);
        if (value != null) {
            putValue(javax.swing.Action.LONG_DESCRIPTION, value);
        }
        value = getPropertyResourceValue(ActionProperties.MNEMONIC);
        if (value != null) {
            putValue(javax.swing.Action.MNEMONIC_KEY, Integer.parseInt(value));
        }
        value = getPropertyResourceValue(ActionProperties.ICON);
        if (value != null) {
            Icon icon = loadIcon(getClass().getClassLoader().getResource(value));
            putValue(javax.swing.Action.SMALL_ICON, icon);
            putValue(javax.swing.Action.LARGE_ICON_KEY, icon);
        }
        value = getPropertyResourceValue(ActionProperties.SMALL_ICON);
        if (value != null) {
            Icon icon = loadIcon(getClass().getClassLoader().getResource(value));
            putValue(javax.swing.Action.SMALL_ICON, icon);
        }
        value = getPropertyResourceValue(ActionProperties.LARGE_ICON);
        if (value != null) {
            Icon icon = loadIcon(getClass().getClassLoader().getResource(value));
            putValue(javax.swing.Action.LARGE_ICON_KEY, icon);
        }
        value = getPropertyResourceValue(ActionProperties.KEYSTROKE);
        if (value != null) {
            putValue(javax.swing.Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(value));
        }
        value = getPropertyResourceValue(ActionProperties.SELECTED);
        if (value != null) {
            putValue(javax.swing.Action.SELECTED_KEY, Boolean.parseBoolean(value));
        }
        value = getPropertyResourceValue(ActionProperties.ENABLED);
        if (value != null) {
            setEnabled(Boolean.parseBoolean(value));
        }

    }

    private static Icon loadIcon(URL url) {
        Icon icon = null;
        if (url != null) {
            try {
                Image image = ImageIO.read(url);
                icon = new ImageIcon(image);
            } catch (IOException ex) {
                LOG.error("could not load icon " + url, ex);
            }
        }
        return icon;
    }

    private String getPropertyResourceValue(String property) {
        String value = null;
        String propertyKey = new StringBuilder(propertyPrefix).append(".").append(property).toString();
        try {
            value = resources.getString(propertyKey);
        } catch (MissingResourceException ex) {
            LOG.trace("property not found: " + propertyKey);
        }
        return value;
    }
}
